package io.renren.runner;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import io.renren.htby.utils.KafkaConsumeUtils;
import io.renren.kafka.consume.EqmStateConsumer;
import io.renren.modules.iots.entity.EqmStateEntity;
import io.renren.modules.iots.service.IotsEquipmentInfoService;

/**
 * 状态 监控 的消费者
 * @author lfy.xys
 * @date 2018年6月1日
 *
 */
//@Component
public class EqmStateConsumerRunner implements CommandLineRunner {

	@Autowired
	private EqmStateConsumer consumer;
	
	@Autowired
	private IotsEquipmentInfoService iotsEquipmentInfoService;
	
	private static Logger logger = LoggerFactory.getLogger(EqmStateConsumerRunner.class);

	/** 设备的集合 */
	public static Map<Long, EqmStateEntity> eqmMap =  new ConcurrentHashMap<Long, EqmStateEntity>();
	
	
	public void run(String... var1) throws Exception {
		logger.info("This will be execute when the project was started!");
		
		// 系统启动，查询所有 设备信息，到map中
		List<EqmStateEntity> queryEqmStateMap = iotsEquipmentInfoService.queryEqmStateMap();
		for (EqmStateEntity entity : queryEqmStateMap) {
			eqmMap.put(entity.getId(), entity);
		}
		
		logger.info(eqmMap.size() + "");
		
		// 消费状态信息
		consumer.consume(KafkaConsumeUtils.eqm_state_topic);
//		consumer.TestConsume();//测试
	}
}
